import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../const/const.dart';

enum SearchItemType { top, bottom, middlle, topBottom }

class SearchItem extends StatefulWidget {
  final DSPeripheral device;
  bool isManager = false;
  SearchItemType type = SearchItemType.middlle;
  Function() onSelect;
  Function() onPressLongToChangeName;
  SearchItem({
    super.key,
    required this.device,
    required this.isManager,
    required this.type,
    required this.onSelect,
    required this.onPressLongToChangeName,
  });

  @override
  State<SearchItem> createState() => _SearchItemState();
}

class _SearchItemState extends State<SearchItem> {
  bool get isconncet {
    return widget.device.peripheral?.result.device.isConnected ?? false;
  }

  @override
  Widget build(BuildContext context) {
    var radius = const BorderRadius.all(Radius.circular(0));
    if (widget.type == SearchItemType.top) {
      radius = const BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10));
    } else if (widget.type == SearchItemType.bottom) {
      radius = const BorderRadius.only(bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10));
    } else if (widget.type == SearchItemType.topBottom) {
      radius = const BorderRadius.all(Radius.circular(10));
    }
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        return GestureDetector(
          // onTap: widget.isManager ? null : () => widget.onSelect(),
          // onLongPress: widget.isManager ? null : () => widget.onPressLongToChangeName(),
          child: Container(
            width: constraints.maxWidth,
            height: 40,
            decoration: BoxDecoration(color: Colors.black, borderRadius: radius),
            clipBehavior: Clip.hardEdge, // 通过clipBehavior来切割子组件
            child: Stack(
              children: [
                ///设备名字
                Positioned(
                  left: 20,
                  top: 0,
                  bottom: 0,
                  child: Center(
                    child: Text(
                      widget.device.peripheral?.result.device.platformName ?? '',
                      style: TextStyle(fontSize: 13, color: isconncet ? themeColor : Colors.white),
                    ),
                  ),
                ),

                ///设备自定义名字
                Positioned(
                  right: 70,
                  top: 0,
                  bottom: 0,
                  child: widget.isManager
                      ? Container(height: 0)
                      : ConstrainedBox(
                          constraints: const BoxConstraints(minWidth: 40, minHeight: 40),
                          child: Center(
                            child: InkWell(
                              onTapUp: (details) => widget.isManager ? null : widget.onPressLongToChangeName(),
                              child: Text(
                                widget.device.name,
                                textAlign: TextAlign.right,
                                style: TextStyle(fontSize: 13, color: isconncet ? Colors.grey : Colors.white),
                              ),
                            ),
                          ),
                        ),
                ),

                ///switch开关
                Positioned(
                  right: 10,
                  top: 6,
                  bottom: 6,
                  child: InkWell(
                    onTapUp: (details) => widget.isManager ? null : widget.onSelect(),
                    child: Image.asset(
                      widget.isManager
                          ? assets.more_select
                          : isconncet
                          ? assets.connect
                          : assets.disconnect,
                      width: 30,
                    ),
                  ),
                ),

                ///下划线
                Positioned(
                  left: 0,
                  right: 0,
                  bottom: 0,
                  child: SizedBox(
                    height: 0.5,
                    child: Container(
                      color: widget.type == SearchItemType.bottom || widget.type == SearchItemType.topBottom
                          ? Colors.transparent
                          : Colors.grey,
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
